﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.GoodsCategory
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    try
                    {
                        Guid ID = new Guid(Request.Params["id"]);
                        BindGoodsCategory();

                        ShowInfo(ID);
                    }
                    catch
                    {
                    }
                }
            }
        }

        protected void BindGoodsCategory()
        {
            Things.BLL.GoodsCategory bll = new BLL.GoodsCategory();
            DataSet ds = bll.GetList(" IsDeleted=0 ");

            ddlGoodsCategory.Items.Clear();
            ddlGoodsCategory.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            Things.Common.UICommon.BindDDLTree(ddlGoodsCategory, ds.Tables[0], Guid.Empty, "&nbsp;&nbsp;&nbsp;");
        }

        private void ShowInfo(Guid ID)
        {
            Things.BLL.GoodsCategory bll = new Things.BLL.GoodsCategory();
            Things.Model.GoodsCategory model = bll.GetModel(ID);
            this.lblID.Text = model.ID.ToString();
            this.txtGoodsCategoryName.Text = model.GoodsCategoryName.ToString();
            this.txtGoodsCategoryLevel.Text = model.GoodsCategoryLevel.ToString();
            this.txtGoodsCategoryOrder.Text = model.GoodsCategoryOrder.ToString();
            Things.Common.UICommon.SetSelectedDDL(ddlGoodsCategory, model.ParentID.ToString());

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (!PageValidate.IsNumber(txtGoodsCategoryLevel.Text))
            {
                strErr += "商品分类层级格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtGoodsCategoryOrder.Text))
            {
                strErr += "商品分类排序格式错误！\\n";
            }
            Guid parentID = Guid.Empty;
            if (this.ddlGoodsCategory.SelectedValue == "-1")
            {
                parentID = Guid.Empty;
            }
            else
            {
                if (Guid.TryParse(this.ddlGoodsCategory.SelectedValue, out parentID) == false)
                {
                    strErr += "父商品分类有误！\\n";
                }
            }

            if (parentID.ToString() == this.lblID.Text)
            {
                strErr += "当前分类与父类相同！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid ID = new Guid(this.lblID.Text);
            string GoodsCategoryName = this.txtGoodsCategoryName.Text;
            Guid ParentID = parentID;
            int GoodsCategoryLevel = int.Parse(this.txtGoodsCategoryLevel.Text);
            int GoodsCategoryOrder = int.Parse(this.txtGoodsCategoryOrder.Text);


            Things.BLL.GoodsCategory bll = new Things.BLL.GoodsCategory();

            Things.Model.GoodsCategory model = bll.GetModel(ID);
            if (model != null)
            {
                model.GoodsCategoryName = GoodsCategoryName;
                model.ParentID = ParentID;
                model.GoodsCategoryLevel = GoodsCategoryLevel;
                model.GoodsCategoryOrder = GoodsCategoryOrder;

                bll.Update(model);
                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
